#!/bin/sh

###############################################################################################
# 1. main函数是CMDB自发现脚本的入口函数
# 2. linux_host_check函数是基于Linux主机的自发现
# 3. 所有基于Linux主机自发现功能，都被包装在linux_check函数中
# 4. linux_host_check函数中的return_result是结果打印函数
# 5. print_comma及其他功能函数辅助return_result拼接Json结果，每个函数获取一个自发现信息
###############################################################################################

#多个IP时，用空格分隔
#ipsegment=''

#基于Linux主机自发现
function linux_host_check {
	#局部变量定义
	local result=""
	local err_msg=""
	function sudo_check {
		if [ `uname` = "Linux" ];then
			if [ `sudo -A ls 1>/dev/null 2>&1;echo $?` -ne 0 ];then
				return_error HAVE_NOT_SUDO_AUTH
				exit 1
			fi
		else
			return_error UNKNOWN_OS
			exit 1
		fi
	}
	#sudo_check
	
	#获取ips
	function host_ip {
		# ipsegment=(`ipaddr`)
		host_ip=`nmap -sP -n $ipsegment| grep -w "Nmap scan report for" | awk '{print $NF}' 2>/dev/null`
		if [ $? -ne 0 ];then
			err_msg="GET_HOSTNAME_FAILURE"
		fi
		if [ "$1" = 'err_msg' ];then
			echo "$err_msg"
			return 1
		else
			if [ "$host_ip" ];then
				echo "$host_ip"
				return 0
			else
				return 1
			fi
		fi
	}
	
	#获取端口
	function get_port {
		ip_port=`nmap ${hostIp}|grep tcp|awk -F'/' '{print $1}' 2>/dev/null`
		if [ $? -ne 0 ];then
			err_msg="GET_HOSTNAME_FAILURE"
		fi
		if [ "$1" = 'err_msg' ];then
			echo "$err_msg"
			return 1
		else
			if [ "$ip_port" ];then
				echo "$ip_port"
				return 0
			else
				return 1
			fi
		fi
	}
	
	#获取状态
	function get_status {
		status=`nmap ${hostIp}|grep tcp|grep ${ipPort}|awk '{print $2}' 2>/dev/null`
		if [ $? -ne 0 ];then
			err_msg="GET_HOSTNAME_FAILURE"
		fi
		if [ "$1" = 'err_msg' ];then
			echo "$err_msg"
			return 1
		else
			if [ "$status" ];then
				echo "$status"
				return 0
			else
				return 1
			fi
		fi
	}
	
	#获取服务
	function get_service {
		service=`nmap ${hostIp}|grep tcp|grep ${ipPort}|awk '{print $NF}' 2>/dev/null`
		if [ $? -ne 0 ];then
			err_msg="GET_HOSTNAME_FAILURE"
		fi
		if [ "$1" = 'err_msg' ];then
			echo "$err_msg"
			return 1
		else
			if [ "$service" ];then
				echo "$service"
				return 0
			else
				return 1
			fi
		fi
	}

	
	#是否打印逗号
	function print_comma {
		if [ $1 -ne $2 ]; then
			echo ","
		else
			if [ $3 -ne $4 ]; then
				echo ","
			fi
		fi
	}

	#打印结果
	function return_result {
		printf \
		'
		{	
			"messageCode": "success",
			"message": "",
			"data": [
		'
		host_ips=($(host_ip))
		num=${#host_ips[@]}
		i=0
		#打印每个实例
		for host_ip in ${host_ips[@]}
		do
			hostIp=$host_ip
			((i++))
			ip_ports=(`get_port`)
			numport=${#ip_ports[@]}
			j=0
			for ip_port in ${ip_ports[@]}
			do
			    ((j++))
				ipPort=$ip_port
				
				echo \
				'{
					"ciCode": "ip_detection",
					"attributes": [
						{
							"attrName": "主机IP",
							"attrCode": "host_ip",
							"attrValue": "'`echo ${hostIp}`'",
							"attrFailureReason": ""
						},{
							"attrName": "端口",
							"attrCode": "port",
							"attrValue": "'`echo ${ipPort}`'",
							"attrFailureReason": ""		
						},{
							"attrName": "状态",
							"attrCode": "port_status",
							"attrValue": "'`get_status`'",
							"attrFailureReason": "'`get_status err_msg`'"		
						}
						,{
							"attrName": "服务",
							"attrCode": "service",
							"attrValue": "'`get_service`'",
							"attrFailureReason": "'`get_service err_msg`'"		
						}
					],
					"relations": []
				}'`print_comma $i $num $j $numport`''
			done
		done
		echo "]}"
	}
	return_result;
}

#错误定义
function return_error {
	case $1 in 
	'UNKNOW_OS')
		printf \
		'
			{	
				"messageCode": "error",
				"message": "unknow os type",
				"data": []
			}\n'
		;;
	'HAVE_NOT_SUDO_AUTH')
		printf \
		'
			{	
				"messageCode": "error",
				"message": "have not sudo auth",
				"data": []
			}\n'
		;;
	'NO_INSTANCE')
		printf \
		'
			{	
				"messageCode": "error",
				"message": "no database",
				"data": []
			}\n'
		;;
	'CMD_EXECUTE_ERROR')
		printf \
		'
			{	
				"messageCode": "error",
				"message": "ps execute error",
				"data": []
			}\n'
		;;
	esac
}

#自发现入口
function main {
	os_type=`uname -s`
	case ${os_type} in
		'Linux')
			linux_host_check
			;;
		'*')
			return_error 'UNKNOW_OS';
			;;
	esac
}

main;
